<div class="contentArea">
    <div class="Item hr">
        <div class="current">test</div>
    </div>
    <form>
        <table width="100%" border="0" cellspacing="0" cellpadding="0" class="table1">
            <tr id="upload_text">
                <th width="90">上传文件：</th>
                <td>
                    <input type="file" id="text" name="text" onchange='UploadImage(this);'>
                </td>
            </tr>


        </table>
    </form>
    <div class="commonBtnArea">
        <button class="btn submit">提交</button>
    </div>
</div>

<script type="text/javascript" src="__PUBLIC__/javascripts/fileupload/ajax_file_upload.js"></script>
<script type="text/javascript" src="__PUBLIC__/javascripts/fileupload/file_upload.js"></script>
<script type="text/javascript" src="__PUBLIC__/javascripts/admin/layer/layer.js"></script>


<script type="text/javascript">

    /**
     * 上传图片
     */
    function UploadImage(event) {
        var fileid = $(event).attr("id");
        var data = {'type':fileid};
        var url = "{:U('Novel/upload')}";

        uploadFile(fileid,data,url,function(data){

            if(data.code == 0){
                var filePath = data.data;
                $.post(
                    "{:U('Novel/parse_text')}",{filePath:filePath},function(json){
                        $('#upload_text').hide();
                        var novel_info = json.data.novel_info;
                        var chapter_info = json.data.chapter_data;

                        var novel_html = "<tr>" +
                            "                <th width='100'>小说名称：</th>" +
                            "                <td><input type='text' class='input' size='50' name='title' value='"+novel_info['title']+"'></td>" +
                            "            </tr>" +
                            " <tr>" +
                            "                <th width='100'>小说作者：</th>" +
                            "                <td><input type='text' class='input' size='50' name='author' value='"+novel_info['author']+"'></td>" +
                            "            </tr>" +
                            " <tr>" +
                            "                <th width='100'>小说字数：</th>" +
                            "                <td><input type='text' class='input' size='50' name='words_number' value='"+novel_info['words_number']+"'></td>" +
                            "            </tr>" +
                            " <tr>" +
                            "                <th width='100'>来源名称：</th>" +
                            "                <td><input type='text' class='input' size='50' name='source_addr' value='"+novel_info['source_addr']+"'></td>" +
                            "            </tr>" +
                            " <tr>" +
                            "                <th width='100'>来源网址：</th>" +
                            "                <td><input type='text' class='input' size='50' name='source_url' value='"+novel_info['source_url']+"'></td>" +
                            "            </tr>" +
                            " <tr>" +
                            "                <th width='100'>小说简介：</th>" +
                            "                <td><textarea type='text' class='jq_watermark' style='width: 400px;height: 80px;' name='intro'>"+novel_info['intro']+"</textarea></td>" +
                            "            </tr>" +
                            " <tr>" ;

                        var chapter_html = '';
                        if (chapter_info.length > 0) {
                            for (var i = 0; i < chapter_info.length; i++) {
                                var item = chapter_info[i];
                                chapter_html += " <tr>" +
                                    "                <th width='100'>章节名称：</th>" +
                                    "                <td><input type='text' class='input' size='50' id='title_"+i+"' value='"+item['title']+"'></td>" +
                                    "            </tr>" ;
                                chapter_html += " <tr>" +
                                    "                <th width='100'>字数：</th>" +
                                    "                <td><input type='text' class='input' size='50' id='words_number_"+i+"' value='"+item['words_number']+"'></td>" +
                                    "            </tr>";
                                chapter_html += " <tr>" +
                                    "                <th width='100'>章节简介：</th>" +
                                    "                <td><textarea type='text' class='jq_watermark' id='content_"+i+"' style='width: 400px;height: 80px;'>"+item['content']+"</textarea></td>" +
                                    "            </tr>";
                            }
                        }

                        $('.table1').append(novel_html+chapter_html);

                        $(".submit").click(function(){
                            //小说信息
                            var novel_data = novel_info;
                            novel_data.title = $('input[name=title]').val();
                            novel_data.author = $('input[name=author]').val();
                            novel_data.words_number = $('input[name=words_number]').val();
                            novel_data.source_addr = $('input[name=source_addr]').val();
                            novel_data.source_url = $('input[name=source_url]').val();
                            novel_data.intro = $('textarea[name=intro]').val();

                            //章节信息
                            var chapter_data = [];
                            for(var i = 0; i < chapter_info.length; i++) {
                                var chapter_item = chapter_info[i];
                                chapter_item.title = $("#title_"+i).val();
                                chapter_item.words_number = $("#words_number_"+i).val();
                                chapter_item.content = $("#content_"+i).val();
                                chapter_data.push(chapter_item);
                            }

                            console.log(novel_data,chapter_data);
                            $.post("{:U('Novel/add_novel')}",{novel_data:novel_data,chapter_data:chapter_data},function(data){
                                if(data.code == 0){
                                    layer.msg(data.message,{icon:1,time: 1000},function(){location.href="Novel/index"});
                                }
                            },'json')
                        });

                    },'json'
                )
            }
        }, 'json');
    }

</script>


